<template>
  <div class="app-container">
    <el-form class="formList" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch"
      label-width="80px">
      <el-row>

        <el-form-item label="分类名称" prop="cmsClassName">
          <el-input v-model="queryParams.cmsClassName" placeholder="请输入分类名称" clearable size="small"
            @keyup.enter.native="handleQuery" />
        </el-form-item>

        <el-form-item label="状态" prop="dataStatus">
          <el-select v-model="queryParams.dataStatus" placeholder="请选择状态" clearable size="small">
            <el-option v-for="dict in dataStatusOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>

        <el-form-item style="margin-left: 22px;">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>

      </el-row>
    </el-form>

    <el-row class="row-mbinfo">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleSave"
          v-hasPermi="['platform:cmsClass:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <div class="divTable">
      <el-table border v-loading="loading" :data="cmsClassList" row-key="cmsClassUid" default-expand-all
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
        <el-table-column label="分类名称" align="center" prop="cmsClassName" />
        <el-table-column label="上级名称" align="center" prop="cmsClassPname" />
        <el-table-column label="等级" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.isCmsLeaf == 0">父级</span>
            <span v-if="scope.row.isCmsLeaf == 1">子级</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="dataStatus">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.dataStatus" active-value="1" inactive-value="0"
              @change="handleStatusChange(scope.row)" :disabled="checkCode(scope.row.cmsClassCode)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="排序" align="center" prop="dataPriority" sortable />
        <el-table-column label="修改时间" align="center" prop="updateTime" width="180" sortable>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime, "{y}-{m}-{d} {h}:{m}:{s}") }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['platform:cmsClass:edit']"  v-if="checkCode(scope.row.cmsClassCode) == false">修改</el-button>
            <el-button v-if="scope.row.isCmsLeaf == 0" size="mini" type="text" icon="el-icon-plus"
              @click="handleAdd(scope.row)" v-hasPermi="['platform:cmsClass:add']">新增
            </el-button>
            <el-button v-if="scope.row.dataStatus == 0" size="mini" type="text" icon="el-icon-delete"
              @click="handleDelete(scope.row)" v-hasPermi="['platform:cmsClass:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 添加或修改信息分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @keyup.enter.native="submitForm">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分类名称" prop="cmsClassName">
          <el-input v-model="form.cmsClassName" placeholder="请输入分类名称" class="select-normal" />
        </el-form-item>

        <el-form-item label="排序" prop="dataPriority">
          <el-input-number v-model="form.dataPriority" size="small" placeholder="请输入排序" controls-position="right"
            :min="0" class="select-normal" :max="999999" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listCmsClass,
    getCmsClass,
    delCmsClass,
    addCmsClass,
    updateCmsClass,
    updateCmsClassStatus,
  } from "@/api/platform/cmsClass";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  export default {
    name: "CmsClass",
    components: {
      Treeselect,
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 信息分类表格数据
        cmsClassList: [],
        // 信息分类树选项
        cmsClassOptions: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 状态(0无效/1有效)字典
        dataStatusOptions: [],
        // 查询参数
        queryParams: {
          cmsClassPuid: null,
          cmsClassCode: null,
          cmsClassName: null,
          cmsClassPname: null,
          isCmsLeaf: null,
          dataStatus: null,
          dataPriority: null,
          createUid: null,
          createName: null,
          createTime: null,
          updateUid: null,
          updateName: null,
          updateTime: null,
          platformUid: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          cmsClassName: [{
            required: true,
            message: "分类名称不能为空！",
            trigger: "blur",
          }, ],
          dataPriority: [{
            required: true,
            message: "排序不能为空！",
            trigger: "blur",
          }, ]
        },
      };
    },
    created() {
      this.getList();
      this.getDicts("sys_data_status").then((response) => {
        this.dataStatusOptions = response.data;
      });
    },
    methods: {
      checkCode(value){
        if(value === "gonggao" || value === "pingtaixinxi" || value === "zixun"){
          return true;
        }else {
          return false;
        }
      },
      /** 查询信息分类列表 */
      getList() {
        this.loading = true;
        listCmsClass(this.queryParams).then((response) => {
          this.cmsClassList = this.handleTree(
            response.data,
            "cmsClassUid",
            "cmsClassPuid"
          );
          this.loading = false;
        });
      },
      /** 转换信息分类数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.cmsClassUid,
          label: node.cmsClassName,
          children: node.children,
        };
      },
      /** 查询信息分类下拉树结构 */
      getTreeselect() {
        listCmsClass().then((response) => {
          this.cmsClassOptions = [];
          const data = {
            cmsClassUid: 0,
            cmsClassName: "顶级节点",
            children: []
          };
          data.children = this.handleTree(
            response.data,
            "cmsClassUid",
            "cmsClassPuid"
          );
          this.cmsClassOptions.push(data);
        });
      },
      // 状态(0无效/1有效)字典翻译
      dataStatusFormat(row, column) {
        return this.selectDictLabel(this.dataStatusOptions, row.dataStatus);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          cmsClassUid: null,
          cmsClassPuid: null,
          cmsClassCode: null,
          cmsClassName: null,
          cmsClassPname: null,
          isCmsLeaf: null,
          dataStatus: null,
          dataPriority: null,
          createUid: null,
          createName: null,
          createTime: null,
          updateUid: null,
          updateName: null,
          updateTime: null,
          platformUid: null,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd(row) {
        this.reset();
        this.form.isCmsLeaf = 1;
        this.form.cmsClassPuid = row.cmsClassUid;
        this.form.cmsClassPname = row.cmsClassName;
        this.open = true;
        this.title = "添加信息分类";
      },
      handleSave() {
        this.reset();
        this.form.isCmsLeaf = 0;
        this.open = true;
        this.title = "添加信息分类";
      },
      // 状态修改
      handleStatusChange(row) {

        let text = row.dataStatus === "0" ? "无效" : "有效";
        this.$confirm('确定要把类别名称为"' + row.cmsClassName + '",设为' + text + '吗？', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return updateCmsClassStatus(row.cmsClassUid, row.dataStatus);
        }).then(() => {
          this.msgSuccess("操作成功");
          this.getList();
        }).catch(function() {
          row.dataStatus = row.dataStatus === "0" ? "1" : "0";
        });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.getTreeselect();
        /* if (row != null) {
           this.form.cmsClassPuid = row.cmsClassUid;
         }*/
        getCmsClass(row.cmsClassUid).then((response) => {
          this.form = response.data;
          this.open = true;
          this.title = "修改信息分类";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate((valid) => {
          if (valid) {
            if (this.form.cmsClassUid != null) {
              updateCmsClass(this.form).then((response) => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addCmsClass(this.form).then((response) => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$confirm(
            '是否确认删除信息分类名称为"' + row.cmsClassName + '"的数据项?',
            "警告", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            }
          )
          .then(function() {
            return delCmsClass(row.cmsClassUid);
          })
          .then(() => {
            this.getList();
            this.msgSuccess("删除成功");
          });
      },
    },
  };
</script>
